<template>
  <div class="message">
    <heads title="聊一聊"></heads>
    <ul v-if="list">
      <li v-for="(item, index) in list" :key="index">
        <p class="time">
          <span>{{ item.date }}</span>
        </p>
        <div class="main flex" :class="{ self: item.self }">
          <img
            class="avatar"
            width="30"
            height="30"
            :src="item.self ? item.img : item.sessionImg"
          />
          <div class="text">{{ item.content }}</div>
        </div>
      </li>
    </ul>
    <div class="place-holder" :style="{ height: height }"></div>
    <div class="btn-box" ref="footer">
      <div class="flex">
        <van-uploader>
          <van-icon name="photo-o" size="30" color="#999" />
        </van-uploader>

        <van-icon name="smile-o" size="30" color="#999" @click="emitShow" />
        <van-field
          v-model="content"
          :border="false"
          label=""
          placeholder="请输入用内容"
        />
        <van-button type="info" round class="btn-common" @click="send">
          发送
        </van-button>
      </div>
      <picker
        v-if="emotion"
        :include="['people']"
        :showSearch="false"
        :showPreview="false"
        :showCategories="false"
        @select="addEmoji"
      />
    </div>
  </div>
</template>
<script>
import { Picker } from "emoji-mart-vue"; //引入组件
export default {
  components: {
    heads: resolve => require(["@/components/common/heads"], resolve),
    Picker
  },
  data() {
    return {
      content: "",
      emotion: false,
      height: "",
      websock: null,
      list: [
        {
          date: "2020-10-11",
          self: true,
          content: "您好",
          img: "https://img.yzcdn.cn/vant/cat.jpeg"
        },
        
      ]
    };
  },
  methods: {
    addEmoji(e) {
      this.content += e.native;
    },
    emitShow() {
      let _this = this;
      _this.emotion = !_this.emotion;
      setTimeout(() => {
        _this.height = `${_this.$refs.footer.offsetHeight}px`;
        console.log(_this.$refs.footer.offsetHeight);
      }, 1000);
    },
    send() {
      let _this = this;
      _this.emotion = false;
       this.websocketsend(this.content);
    },
    initWebSocket(){ //初始化weosocket
        const wsuri = "ws://www.rmcypt.com:8666/ws";
        this.websock = new WebSocket(wsuri);
        this.websock.onmessage = this.websocketonmessage;
        this.websock.onopen = this.websocketonopen;
        this.websock.onerror = this.websocketonerror;
        this.websock.onclose = this.websocketclose;
      },
      websocketonopen(){ //连接建立之后执行send方法发送数据
        this.websocketsend(this.content);
      },
      websocketonerror(){//连接建立失败重连
      console.log('连接失败',e);
        this.initWebSocket();
      },
      websocketonmessage(e){ //数据接收
      console.log('数据接收',e)
        const redata = JSON.parse(e.data);
      },
      websocketsend(Data){//数据发送
        this.websock.send(Data);
      },
      websocketclose(e){  //关闭
        console.log('断开连接',e);
      },
  },
  created() {
    let _this = this;
_this.initWebSocket();
    setTimeout(() => {
      _this.height = `${_this.$refs.footer.offsetHeight}px`;
      console.log(_this.$refs.footer.offsetHeight);
    }, 1000);
  },
   destroyed() {
      this.websock.close() //离开路由之后断开websocket连接
    },
};
</script>

<style lang="scss" scoped>
.message {
  padding: 10px 15px;
  overflow-y: scroll;

  li {
    margin-bottom: 15px;
  }
  .time {
    margin: 7px 0;
    text-align: center;
    font-size: 0;

    > span {
      display: inline-block;
      padding: 0 18px;
      font-size: 12px;
      color: #999;
      border-radius: 2px;
    }
  }
  .avatar {
    margin: 0 10px 0 0;
    border-radius: 3px;
  }
  .text {
    display: inline-block;
    position: relative;
    padding: 0 10px;
    max-width: calc(100% - 40px);
    min-height: 30px;
    line-height: 2.5;
    font-size: 12px;
    text-align: left;
    word-break: break-all;
    background-color: #fff;
    border-radius: 4px;

    &:before {
      content: " ";
      position: absolute;
      top: 9px;
      right: 100%;
      border: 6px solid transparent;
      border-right-color: #fff;
    }
  }
  .main {
    justify-content: flex-start;
    &.self {
      // justify-content: flex-end!important;
      text-align: right;
      flex-flow: row-reverse;

      .avatar {
        margin: 0 0 0 10px;
      }
      .text {
        background-color: #b2e281;

        &:before {
          right: inherit;
          left: 100%;
          border-right-color: transparent;
          border-left-color: #b2e281;
        }
      }
    }
  }
}

.btn-box {
  background: #fff;
  padding: 0.1rem 0.3rem;
  position: fixed;
  width: 100%;
  box-sizing: border-box;
  left: 0;
  bottom: 0;
  .van-field {
    background: #f7f7f7;
    border-radius: 30px;
    margin-right: 0.3rem;
  }
  /deep/ .van-uploader {
    height: 0.7rem;
    .van-uploader__wrapper {
      height: 100%;
    }
    .van-uploader__input-wrapper {
      height: 100%;
      display: flex;
      align-items: center;
    }
  }
  .van-icon {
    margin-right: 0.1rem;
  }
  .btn-common {
    border-radius: 0.1rem;
    height: 0.6rem;
    line-height: 0.6rem;
    font-size: 0.3rem;
    width: auto;
    padding: 0;
    width: 1.7rem;
  }
  .emoji-mart {
    border: none;
    height: 3rem;
    transition: all 0.1s;
    /deep/ .emoji-mart-category-label {
      display: none;
    }
  }
}
</style>
